//
//  ThrViewController.m
//  HomeFurnishing
//
//  Created by dllo on 16/4/7.
//  Copyright © 2016年 YMing. All rights reserved.
//

#import "ThrViewController.h"
#import "ThrVCCollectionViewCell.h"
#import "CaseBaseModel.h"
#import "DetailThridVC.h"
@interface ThrViewController ()<UICollectionViewDelegate, UICollectionViewDataSource,ARSegmentControllerDelegate>
@property(nonatomic, retain)UICollectionView *collectionV;
@property(nonatomic, retain)NSArray *dataArr;
@end

@implementation ThrViewController
-(NSString *)segmentTitle
{
    return [NSString stringWithFormat:@"%@ 灵感集",self.num];
}
-(UIScrollView *)streachScrollView
{
    return self.collectionV;
}

-(void)dealloc
{
    [_collectionV release];
    [_name release];
    [_dataArr release];
    [_num release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.dataArr = [NSArray array];
    [self createCollectionV];
    [self dataHandle];
}
-(void)createCollectionV
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 20, 0, 10);
    layout.itemSize = CGSizeMake((SCREEN_WIDTH - 20 - 40)/2, 150);
    self.collectionV = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    self.collectionV.backgroundColor = [UIColor whiteColor];
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    [self.view addSubview:self.collectionV];
    [self.collectionV registerClass:[ThrVCCollectionViewCell class] forCellWithReuseIdentifier:@"ThrVCCollectionVCell"];
    [_collectionV release];
}
-(void)dataHandle
{
    NSString *str = [[NSString stringWithFormat:@"http://api.guju.com.cn/v2/user/%@/ideabooks?start=0&count=10&user=木木",self.name]stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    [NetworkHandle GetDataWithURL:str dic:nil success:^(id responseObject) {
        self.dataArr = [CaseBaseModel transformWithArray:responseObject[@"ideabooks"]];
        [self.collectionV reloadData];
    } filed:^(NSError *error) {
        
    }];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ThrVCCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ThrVCCollectionVCell" forIndexPath:indexPath];
    CaseBaseModel *m = self.dataArr[indexPath.row];
    cell.title.text = m.title;
    NSArray *arr = m.photos;
    NSString *str = [NSString stringWithFormat:@"http://gooju.cn/dimages/%@_0_w750_h398_m0.jpg",arr[0][@"id"]];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:str]];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailThridVC *VC = [[DetailThridVC alloc] init];
    CaseBaseModel *m = self.dataArr[indexPath.row];
    VC.dataArr = m.photos;
    VC.navigationItem.title = m.title;
    [self.navigationController pushViewController:VC animated:YES];
    [VC release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
